<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="航班号" prop="flightNumber">
        <el-input
          v-model="queryParams.flightNumber"
          placeholder="请输入航班号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出发机场" prop="departureAirportId">
        <el-select v-model="queryParams.departureAirportId" placeholder="请选择出发机场" clearable>
          <el-option
            v-for="dict in dict.type.airport_id"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="到达机场" prop="arrivalAirport">
        <el-select v-model="queryParams.arrivalAirportId" placeholder="请选择到达机场" clearable>
          <el-option
            v-for="dict in dict.type.airport_id"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出发时间" prop="departureTime">
        <el-date-picker clearable
                        v-model="queryParams.departureTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择出发时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="到达时间" prop="arrivalTime">
        <el-date-picker clearable
                        v-model="queryParams.arrivalTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择到达时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="飞机类型" prop="aircraftType">
        <el-select v-model="queryParams.aircraftType" placeholder="请选择飞机类型" clearable>
          <el-option
            v-for="dict in dict.type.aircraft_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="航班状态" prop="aircraftStatus">
        <el-select v-model="queryParams.aircraftStatus" placeholder="请选择航班状态" clearable>
          <el-option
            v-for="dict in dict.type.aircraft_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="flightsList">
      <el-table-column label="航班号" align="center" prop="flightNumber" />
      <el-table-column label="出发机场" align="center" prop="departureAirportId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.airport_id" :value="scope.row.departureAirportId"/>
        </template>
      </el-table-column>
      <el-table-column label="到达机场" align="center" prop="arrivalAirportId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.airport_id" :value="scope.row.arrivalAirportId"/>
        </template>
      </el-table-column>
      <el-table-column label="出发时间" align="center" prop="departureTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.departureTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到达时间" align="center" prop="arrivalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arrivalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="飞机类型" align="center" prop="aircraftType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.aircraft_type" :value="scope.row.aircraftType"/>
        </template>
      </el-table-column>
      <el-table-column label="航班状态" align="center" prop="aircraftStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.aircraft_status" :value="scope.row.aircraftStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="座位" align="center" prop="availableSeats" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >订票</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="changeOfSignature(scope.row)"
          >改签至此航班</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 添加或修改航班信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="航班ID" prop="airlineId">
          <el-input v-model="form.flightId" placeholder="航班ID" :disabled="true" />
        </el-form-item>
        <el-form-item label="航班号" prop="airlineId">
          <el-input v-model="flightNumber" placeholder="航班号" :disabled="true" />
        </el-form-item>
        <el-form-item label="座位类型" prop="seatType">
          <el-select v-model="form.seatType" placeholder="请选择座位类型">
            <el-option
              v-for="dict in dict.type.seat_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="票价" prop="airlineId">
          <el-input v-model="price[form.seatType]" placeholder="票价" :disabled="true" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="pay=true">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="支付界面" :visible.sync="pay" width="500px" append-to-body>
      <!--支付宝收款码-->
      <div style="text-align: center">请支付</div>
      <image-preview src="https://ts1.cn.mm.bing.net/th/id/R-C.1e5871c9b34fc3bf72af08f3ca9cf38c?rik=izMAIOABpwShEA&riu=http%3a%2f%2fcache.mobanma.com%2fUploads%2fpro%2fcover%2f2017%2f02%2f08%2f201702082213457740FemOPA.png&ehk=c%2bvuGxzjrtyPEFLGt%2fo%2fARfphuxmo7mlhEp0x7tZshs%3d&risl=&pid=ImgRaw&r=0" :width="50" :height="50"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="pay=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addFlights, delFlights, getFlights, listFlights, toChangeTickets , toGetTickets} from "@/api/airline/flights";

export default {
  name: "Flights",
  dicts: ['aircraft_status', 'airport_id', 'aircraft_type','ticket_status', 'seat_type'],
  data() {
    return {
      pay: false,
      price:[1000,800,300],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 航班信息表格数据
      flightsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        airlineId: null,
        flightNumber: null,
        departureAirportId: null,
        arrivalAirportId: null,
        departureTime: null,
        arrivalTime: null,
        aircraftType: null,
        aircraftStatus: null,
        availableSeats: null
      },
      // 表单参数
      form: {},
      flightNumber:''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    submitForm(){
      this.form.passengerId = this.$store.getters.userId;
      if (this.title === "订票"){
        toGetTickets(this.form).then(response => {
          if(response.data == null){
            this.$modal.msgSuccess("订票成功");
            this.open = false;
            this.pay = false;
            this.getList();
          }else{
            this.$modal.msgError("订票失败");
          }
        })
      }else{
        toChangeTickets(this.form).then(response => {
          if(response.data == null){
            this.$modal.msgSuccess("改签成功");
            this.open = false;
            this.pay = false;
            this.getList();
          }else{
            this.$modal.msgError("改签失败");
          }
        })
      }

    },
    /** 订票按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.flightNumber = row.flightNumber;
      this.form.flightId = row.flightId;
      this.title = "订票";
      if(this.form.seatType == null){
        this.$message.error("请先选择座位类型");
      }
      this.open = true;
    },
    /** 改签按钮操作 */
    changeOfSignature(row) {
      this.reset();
      this.flightNumber = row.flightNumber;
      this.form.flightId = row.flightId;
      this.title = "改签";
      if(this.form.seatType == null){
        this.$message.error("请先选择座位类型");
      }
      this.open = true;
    },
    /** 查询航班信息列表 */
    getList() {
      this.loading = true;
      listFlights(this.queryParams).then(response => {
        this.flightsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        flightId: null,
        airlineId: null,
        flightNumber: null,
        departureAirportId: null,
        arrivalAirportId: null,
        departureTime: null,
        arrivalTime: null,
        aircraftType: null,
        aircraftStatus: null,
        availableSeats: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>
